package com.example.factory.filter;

import lombok.AllArgsConstructor;

import java.util.LinkedList;
import java.util.List;

public class ChainOfResponsibility {
    public static void main(String[] args) {
        FilterChain filterChain = new FilterChain();
        filterChain.addFilter(new HtmlStep()).addFilter(new FaceStep());
        filterChain.doFilter(new Request("req"),new Response("nse"));

    }
}

interface Step {
    boolean doFilter(Request request, Response response, FilterChain chain);
}

class FilterChain{
    List<Step> stepChain = new LinkedList<>();
    int index = 0;

    FilterChain addFilter(Step step) {
        stepChain.add(step);
        return this;
    }

    public boolean doFilter(Request request, Response response) {
        if(index == this.stepChain.size())return false;
        Step step = this.stepChain.get(index++);
        if(step !=null)
        step.doFilter(request, response, this);
        return true;
    }

}

class HtmlStep implements Step {

    @Override
    public boolean doFilter(Request request, Response response, FilterChain chain) {
        System.out.println(request.msg + " html filter start");
        chain.doFilter(request, response);
        System.out.println(response.msg + " html filter end");
        return true;
    }
}

class FaceStep implements Step {

    @Override
    public boolean doFilter(Request request, Response response, FilterChain chain) {
        System.out.println(request.msg + "Face Filter start ");
        chain.doFilter(request, response);
        System.out.println(response.msg + " Face Filter end ");
        return true;
    }
}

class ServletStep implements Step {

    @Override
    public boolean doFilter(Request request, Response response, FilterChain chain) {
        System.out.println(request.msg + " ServletFilter Filter start ");
        chain.doFilter(request, response);
        System.out.println(response.msg + " ServletFilter Filter end ");
        return true;
    }
}

@AllArgsConstructor
class Request {
    String msg;
}

@AllArgsConstructor
class Response {
    String msg;
}